/*
* Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:flutter/material.dart';

class MyWidget2 extends YPage {
  var title = "";
  var url =
      "https://q2.itc.cn/q_70/images03/20240807/9efb7d3e616440c6ab1d7e1d9b9be14f.jpeg";
  bool _showFirst = true;

  @override
  void yOnCreate() {
    setState(() => title = yGetAppName());
    yBroadcast_register("MyBroadcast", (params) => yToast(params!["msg"]));
  }

  @override
  void initState() {
    super.initState();
    yBroadcast_send("MyBroadcast", params: {"msg": "initState"});
  }

  @override
  void yOnDestory() {
    super.yOnDestory();
    yBroadcast_send("MyBroadcast", params: {"msg": "yOnDestory"});
  }

  @override
  Future yOnYFreeCall(String method, Map arguments) {
    return super.yOnYFreeCall(method, arguments);
  }

  @override
  void yOnReceiveBroadcast(String broadCastName, Map arguments) {
    super.yOnReceiveBroadcast(broadCastName, arguments);
  }

  @override
  bool ySafeAreaBottom() {
    return false;
  }

  Widget view_button(String title, Function() onTap) {
    return YButton(
      child: Text(title,
          style: const TextStyle(
              color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      color: YConfig.themeColor,
      onTap: onTap,
    );
  }

  @override
  Widget yBuild(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('YRight Example')),
      body: SingleChildScrollView(
        child: Column(
          children: [
            YButton(
              isInk: true,
              child: Row(
                children: [
                  Icon(Icons.directions_ferry, size: 20),
                  YSpacer(width: 2),
                  Text("YButton"),
                ],
              ),
              onTap: () => {},
            ),
            YFlatButton(Text("YFlatButton"),
                onClick: () => {
                      yBroadcast_send("MyBroadcast",
                          params: {"msg": "YFlatButton"})
                    }),
            YInkButton(Text("YInkButton"), color: Colors.red, onTap: () {}),
            Row(
              children: [
                Text('YInkRoundButton : '),
                YInkRoundButton(Icon(Icons.close, color: Colors.red),
                    onTap: () {})
              ],
            ),
            YRight(
              child: Container(
                color: Colors.blue,
                width: 100,
                height: 50,
                child: Center(
                  child: Text(
                    'Right',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
            Row(
              children: [
                YEmpty(),
                YEmptyContainer(),
                YExpanded(),
                Container(
                  color: Colors.yellow,
                  width: 100,
                  height: 100,
                  child: Center(
                    child: Text(
                      'YEmpty ,YEmptyContainer ,YExpanded',
                      style: TextStyle(color: Colors.lightBlueAccent),
                    ),
                  ),
                )
              ],
            ),
            Row(children: [
              Text('YSpacer'),
              YSpacer(width: 10, height: 40),
            ]),
            Row(children: [
              Container(
                color: Colors.yellow,
                width: 200,
                height: 50,
                child: Center(
                  child: YTitleWidget('YTitleWidget title'),
                ),
              )
            ]),
            Row(children: [
              Container(
                color: Colors.yellow,
                width: 200,
                height: 50,
                child: Center(child: YAppBarWidget(title: 'YAppBarWidget')),
              )
            ]),
            YImage(100, 100, url),
            YSpacer(width: 10, height: 10),
            YImageCorner(100, 100, url, 10),
            YSpacer(width: 10, height: 10),
            YImageCircle(100, 100, url),
            YSpacer(width: 10, height: 10),
            YImage2(100, 100, url),
            YSpacer(width: 10, height: 10),
            YImageCorner2(100, 100, url, 10),
            YSpacer(width: 10, height: 10),
            YImageCircle2(100, 100, url),
            YSpacer(width: 10, height: 10),
            Row(children: [
              Container(
                color: Colors.yellow,
                height: 150,
                width: 200,
                child: Center(
                    child: YRefreshContainer(
                        child: Text('YRefreshContainer'),
                        onRefresh: () {
                          yToast('YRefreshContainer onRefresh!');
                        })),
              )
            ]),
            Row(children: [
              YSpacer(width: 10, height: 20),
            ]),
            Row(children: [
              Container(
                color: Colors.yellow,
                height: 150,
                width: 200,
                child: Center(
                    child: YRefreshListView(
                        models: [
                      'YRefreshListView',
                      'YListTile1',
                      'YListTile2',
                      'YListTile3'
                    ],
                        itemBuilder:
                            (BuildContext context, int index, String model) {
                          return YListTile(model, null, () {
                            yToast(model + ' onClick');
                          });
                        },
                        onRefresh: () {
                          yToast('YRefreshListView onRefresh!');
                        })),
              )
            ]),
            Row(children: [
              YSpacer(width: 10, height: 20),
            ]),
            Row(children: [
              Container(
                color: Colors.yellow,
                height: 150,
                width: 250,
                child: Center(
                    child: YRefreshStaticListView(
                  children: <Widget>[
                    ListTile(title: Text('YRefreshStaticListView')),
                    ListTile(title: Text('ListTile 2')),
                    ListTile(title: Text('ListTile 3')),
                    ListTile(title: Text('ListTile 4')),
                  ],
                  onRefresh: () {
                    // Handle refresh logic
                  },
                  onLoadMore: () {
                    // Handle load more logic
                  },
                  padding: EdgeInsets.all(8),
                  scrollbar: true,
                )),
              )
            ]),
            Row(children: [
              YSpacer(width: 10, height: 20),
            ]),
            Row(children: [
              Container(
                color: Colors.yellow,
                height: 150,
                width: 200,
                child: Center(
                    child: YRefreshGridView<String>(
                  models: [
                    'YRefreshGridView',
                    'Item 1',
                    'Item 2',
                    'Item 3',
                    'Item 4'
                  ],
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 8.0,
                    crossAxisSpacing: 8.0,
                  ),
                  onRefresh: () {
                    yToast('YRefreshGridView onRefresh!');
                  },
                  onLoadMore: () {},
                  itemBuilder: (context, index, model) {
                    return Card(
                      child: Center(
                        child: Text(model),
                      ),
                    );
                  },
                  padding: EdgeInsets.all(8),
                  scrollbar: true,
                )),
              )
            ]),
            Row(children: [
              YSpacer(width: 10, height: 20),
            ]),
            YCircularProgress(),
            Row(children: [
              YSpacer(width: 10, height: 20),
            ]),
            Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  YAnimatedSwitcher(
                    child: _showFirst
                        ? Text('First Text', key: ValueKey(1))
                        : Text('Second Text', key: ValueKey(2)),
                    duration: 500,
                  ),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        _showFirst = !_showFirst;
                      });
                    },
                    child: Text('Switch Text'),
                  ),
                ],
              ),
            ),
            Row(children: [
              YSpacer(width: 10, height: 44),
            ]),
          ],
        ),
      ),
    );
  }
}
